package cn.njxzc.book.masterexamples.ch20;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import com.mysql.jdbc.Statement;

public class ThreadMysql {

	public static void main(String[] args) {
		ThreadPoolExecutor executor = new ThreadPoolExecutor(5, 15, 200,
				TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(5));
		long start = System.currentTimeMillis();
		System.out.println("activeCountMain1 : " + Thread.activeCount());
		for (int i = 1; i <= 20; i++) {
			MySQL mysql = new MySQL(i);
			executor.execute(mysql);
			System.out.println("线程池中线程数目：" + executor.getPoolSize()
					+ "，队列中等待执行的任务数目：" + executor.getQueue().size()
					+ "，已执行玩别的任务数目：" + executor.getCompletedTaskCount());
		}
		executor.shutdown();
		while (true) {
			if (executor.getActiveCount() == 0)
				break;
		}
		System.out.println("activeCountMain2 : " + Thread.activeCount());
		long end = System.currentTimeMillis();
		System.out.println("平均每秒可输出: " + 100000 / (end - start) + " 条");
	}
}

class MySQL implements Runnable {
	private Connection con = null;
	private static String driver = "com.mysql.jdbc.Driver";
	private static String url = "jdbc:mysql://localhost:3306/test";
	private static String username = "root";
	private static String password = "123456";
	private static Statement NULL = null;
	private final int taskNum;

	public MySQL(int taskNum) {
		this.taskNum = taskNum;
	}

	public Statement MysqlOpen() {
		try {
			Class.forName(driver); // 加载驱动类
			con = DriverManager.getConnection(url, username, password); // 连接数据库
			if (!con.isClosed())
				System.out.println("***数据库成功连接***");
			Statement state = (Statement) con.createStatement();
			return state;
		} catch (ClassNotFoundException e) {
			System.out.println("找不到驱动程序类，加载驱动失败");
			e.printStackTrace();
		} catch (SQLException e) {
			System.out.println("数据库连接失败");
			e.printStackTrace();
		}
		return NULL;
	}

	@Override
	public void run() {
		readMySQL();
	}

	public void readMySQL() {
		ResultSet sql = null;
		Statement state = MysqlOpen();
		try {
			sql = state
					.executeQuery("select * from test where id between "
							+ ((taskNum - 1) * 5000)
							+ " and "
							+ (taskNum * 5000));
			System.out.println("---------task " + taskNum + "正在执行---------");
			while (sql.next()) {
				String id = sql.getString(1);
				String wid = sql.getString(2);
				String username = sql.getString(3);
				String repostscount = sql.getString(4);
				String commentscount = sql.getString(5);
				String text = sql.getString(6);
				String createat = sql.getString(7);
				String source = sql.getString(15);
				String lasttime = sql.getString(17);
				System.out.println(id + "\t" + wid + "\t" + username + "\t"
						+ repostscount + "\t" + commentscount + "\t" + text
						+ "\t" + createat + "\t" + source + "\t" + lasttime);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				sql.close();
				state.close();
				con.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		System.out.println("---------task " + taskNum + "执行完毕---------");
	}
}
